<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Http\Controllers\ElasticsearchServiceController;
use App\Http\Controllers\LibApi\StickFigureController;
use App\Http\Controllers\Resource\ZwzxController;
use App\Models\Digital;
use App\Models\ResourceBanner;
use App\Models\ResourceBusinessName;
use App\Models\ResourceCollect;
use App\Models\ResourceHotSearch;
use App\Models\ResourceTotal;
use App\Models\ResourceTotalType;
use App\Models\ResourceType;
use App\Models\ResourceTypeExternal;
use App\Models\ResourceYtys;
use App\Models\ResourceZwzx;
use App\Models\UserInfo;
use App\Validate\ResourceTotalValidate;

/**
 * 数字资源(资源检索)
 */
class ResourceTotalSearchController extends Controller
{
    public $index = 'cqst_resource';
    public $model = null;
    public $typeModel = null;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->model = new ResourceTotal();
        $this->typeModel = new ResourceType();
        $this->validate = new ResourceTotalValidate();
    }

    /**
     * 检索资源
     * @param keywords 检索数据
     */
    public function resourceSearch()
    {
        $keywords = trim($this->request->keywords);
        if (empty($keywords) || $keywords == '*') {
            return $this->returnApi(201, '请输入检索条件');
        }
        $resourceTypeModel = new ResourceType();
        $resource_type = $resourceTypeModel->where('is_del', 1)->where('number', '>', 0)->get()->toArray();
        if (empty($resource_type)) {
            return $this->returnApi(203, '暂无数据');
        }
        $data = [];
        $i = 0;
        $is_exists_stick_figure = false;
        foreach ($resource_type as $key => $val) {
            if ($val['id'] == 999) {
                $is_exists_stick_figure = true;
                continue;
            }
            $resource_data = $this->model->getResourceListByBigTypeId($val['id'], $keywords, 4);
            if ($resource_data) {
                $data[$i]['big_type_id'] = $val['id'];
                $data[$i]['type_name'] = $val['type_name'];
                $data[$i]['img'] = $val['img'];
                $data[$i]['data'] = $resource_data;
                $i++;
            }
        }

        //检索汇画资源
        if ($is_exists_stick_figure) {
            $stickFigureObj = new StickFigureController();
            $pictures_data = $stickFigureObj->getProductList($keywords, null, 1, 7);
            if ($pictures_data['code'] == 200) {
                $data[$i]['big_type_id'] = 999;
                $data[$i]['type_name'] = '汇画';
                $data[$i]['img'] = $val['img'];
                $data[$i]['data'] = $pictures_data['content']['data'];
                $i++;
            }
        }

        if ($data) {
            return $this->returnApi(200, '获取成功', true, $data);
        }
        return $this->returnApi(203, '暂无数据');
    }


    /**
     * 检索资源（根据类型检索）（除汇画外的资源检索）
     * @param big_type_id 大类
     * @param type_id 小类
     * @param keywords 检索数据
     * @param page  页数，默认为1，
     * @param limit  条数，默认显示 10条
     */
    public function resourceList()
    {
        $keywords = trim($this->request->keywords);
        $page = intval($this->request->input('page', 1)) ?: 1;
        $limit = intval($this->request->input('limit', 10)) ?: 10;
        $big_type_id = $this->request->big_type_id;
        $type_id = $this->request->type_id;

        if ($big_type_id == 999) {
            return $this->returnApi(201, '参数错误');
        }
        // if (empty($keywords) || $keywords == '*') {
        //     return $this->returnApi(201, '请输入检索条件');
        // }

        //先使用ES检索，如果ES异常，则采用mysql进行检索   数据量小，就采用mysql获取数据了
        // try {
        //     $esObj = new ElasticsearchServiceController();
        //     $res = $esObj->esDataSearch($this->index, $keywords, $page, $limit);

        // } catch (\Exception $e) {
        //     echo $e->getMessage();
        //     //采用mysql检索
        //     $res = $this->model->lists($keywords, null, null, null, null, $limit);
        // }

        $type_id_arr = [];
        if ($type_id) {
            $resourceTypeExternalModel = new ResourceTypeExternal();
            $type_id_arr = $resourceTypeExternalModel->getTypeNameAllByTypeId($type_id);
        }
        $res = $this->model->lists($keywords, null, $big_type_id, $type_id_arr, null, null,null, null, null, null, null, $limit);

        if ($res['data']) {
            //添加检索热词
            if (!empty($keywords)) {
                $resourceHotSearch = new ResourceHotSearch();
                $resourceHotSearch->hotSearchAdd($keywords, 2);
            }
            foreach ($res['data'] as $key => $val) {
                $res['data'][$key]['big_type_name'] = !empty($val['con_big_type']) ? $val['con_big_type']['type_name'] : '';
                unset($res['data'][$key]['con_big_type']);
            }

            $res = $this->disPageData($res);
            return $this->returnApi(200, '获取成功', true, $res);
        }
        return $this->returnApi(203, '暂无数据');
    }


    /**
     *  检索资源（根据类型检索）（只检索汇画资源）
     * @param big_type_id 大类    999 只能是
     * @param type_id 小类
     * @param keywords 检索数据
     * @param page  页数，默认为1，
     * @param limit  条数，默认显示 10条
     */
    public function resourcePicturesList()
    {
        $keywords = trim($this->request->keywords);
        $page = intval($this->request->input('page', 1)) ?: 1;
        $limit = intval($this->request->input('limit', 10)) ?: 10;
        $big_type_id = $this->request->big_type_id;
        $type_id = $this->request->type_id;
        if ($big_type_id != 999) {
            return $this->returnApi(201, '参数错误');
        }

        //检索汇画资源
        $stickFigureObj = new StickFigureController();
        $data = $stickFigureObj->getProductList($keywords, $type_id, $page, $limit);
        if ($data['code'] == 200) {
            //添加检索热词
            if (!empty($content)) {
                $resourceHotSearch = new ResourceHotSearch();
                $resourceHotSearch->hotSearchAdd($keywords, 2);
            }
            return $this->returnApi(200, '获取成功', true, $data['content']);
        }
        return $this->returnApi($data['code'], $data['msg']);
    }




    /**
     * 检索热词列表
     * @param page  页数，默认为1，
     * @param limit  条数，默认显示 10条
     */
    public function hotSearchList()
    {
        // if (!$this->validate->scene('hot_search_list')->check($this->request->all())) {
        //     return $this->returnApi(201,  $this->validate->getError());
        // }
        $page = intval($this->request->input('page', 1)) ?: 1;
        $limit = intval($this->request->input('limit', 10)) ?: 10;
        $resourceHotSearch = new ResourceHotSearch();
        $res = $resourceHotSearch->lists(null, $limit);
        if ($res['data']) {
            //增加排名
            foreach ($res['data'] as $key => $val) {
                $res['data'][$key]['rank'] = ($page - 1) * $limit + $key + 1;
            }
            $res = $this->disPageData($res);
            return $this->returnApi(200, '获取成功', true, $res);
        }
        return $this->returnApi(203, '暂无数据');
    }
}
